package com.gdiot.smart_scale;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class SellActivity extends AppCompatActivity {

    RecyclerView RV_GOODS,RV_SELL_LIST;
    List<goods> goodsList = new ArrayList<>();
    List<sell_list> sell_lists = new ArrayList<>();
    String BmobKey = "7992bedadd9805f702701fe85df86a53";
    TextView TX_WEIGHT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sell);
        RV_GOODS = findViewById(R.id.recycler_goods);
        RV_SELL_LIST = findViewById(R.id.sell_list);

        Bmob.initialize(this , BmobKey);

        init_data();

        FragmentManager fragmentManager = this.getSupportFragmentManager();
        Show_Weight show_weight = (Show_Weight) fragmentManager.findFragmentById(R.id.sell_text_weight);
        TX_WEIGHT = show_weight.getView().findViewById(R.id.show_weight);


    }

    private void init_data() {
        //从Bmob云数据获取所有的商品信息，然后保存到goodsList里面。
        String BQL = "select * from goods";
        new BmobQuery<goods>().doSQLQuery(BQL,new SQLQueryListener<goods>(){

            @Override
            public void done(BmobQueryResult<goods> result, BmobException e) {
                if(e ==null){
                    goodsList = result.getResults();
                    if(goodsList!=null && goodsList.size()>0){
                        Log.d("TEST", "get Bmob OK");
                        GoodsRecycerAdapter goodsRecycerAdapter = new GoodsRecycerAdapter(SellActivity.this,goodsList);
                        GridLayoutManager manager = new GridLayoutManager(SellActivity.this , 5);
                        RV_GOODS.setLayoutManager(manager);


                        // 设置数据后就要给RecyclerView设置点击事件
                        goodsRecycerAdapter.setOnItemClickListener(new GoodsRecycerAdapter.ItemClickListener() {
                            @Override
                            public void onItemClick(int position) {
                                // 这里本来是跳转页面 ，我们就在这里直接让其弹toast来演示
                                Toast.makeText(SellActivity.this , goodsList.get(position).getName() , Toast.LENGTH_SHORT).show();
                                String name = goodsList.get(position).getName();
                                String price = goodsList.get(position).getGoodsPrice();
                                String weight = TX_WEIGHT.getText().toString();
                                sell_list list = new sell_list(name , price, weight,"0");
                                sell_lists.add(list);

                                SellList_Adapter sellList_adapter = new SellList_Adapter(sell_lists);
                                LinearLayoutManager manager_selllist = new LinearLayoutManager(SellActivity.this);
                                RV_SELL_LIST.setLayoutManager(manager_selllist);
                                RV_SELL_LIST.setAdapter(sellList_adapter);
                            }
                        });
                        RV_GOODS.setAdapter(goodsRecycerAdapter);

                     }else{
                        Log.i("smile", "查询成功，无数据返回");
                    }
                }else{
                    Log.i("smile", "错误码："+e.getErrorCode()+"，错误描述："+e.getMessage());
                }
            }
        });

    }


}
